#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <algorithm>

void CreateTest(std::vector<int>* array) {
    FILE* fd = fopen("test.txt", "w+");
    srand(time(NULL));
    for(int i = 0; i < 100000; i++) {
        int buf = rand();
        array->push_back(buf);
        fprintf(fd, "%d,", buf);
    }
    fclose(fd);
}


int main(int argc, char** argv) {
    if(argc < 2) return -1;

    std::vector<int> array;
    CreateTest(&array);
    pid_t ischild = fork();
    if(ischild == 0) {
        //execl(argv[1], argv[1], "test.txt", "out.txt", NULL);
    } else {
        int status;
        int w = waitpid(ischild, &status, 0);
        if (w == -1) {
           printf("failed\n");
        }

        if (WIFEXITED(status)) {
            printf("exited, status=%d\n", WEXITSTATUS(status));
        } else if (WIFSIGNALED(status)) {
            printf("killed by signal %d\n", WTERMSIG(status));
        } else if (WIFSTOPPED(status)) {
            printf("stopped by signal %d\n", WSTOPSIG(status));
        } else if (WIFCONTINUED(status)) {
            printf("continued\n");           
        }
                    
        //FILE* fd = fopen("out.txt", "r+");
        std::sort(array.begin(), array.end());
        for(int i = 0; i < array.size(); i++) {
            int temp;
            //fscanf(fd, "%d,", &temp);
            //printf("%d %d\n", temp, array[i]);
            //if(temp != array[i]) printf("failed\n");
        }
        //fclose(fd);
    }
}
    
